﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.OrgModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.Platform
{

    public partial class EnterpriseServiceImp
    {
        #region 组织层级

        #region 保存组织层级

        /// <summary>
        /// 保存组织层级
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Logging("保存组织层级“{@p0.Title}”")]
        public Result SaveOrgLevel(OrgLevelDTO model)
        {
            Result res = new Result();
            var info = model.To<OrgLevel>();
            if (model.Id.IsEmpty())
            {
                _orgLevelRepository.Add(info);
            }
            else
            {
                _orgLevelRepository.AutoUpdateFields(info).Update(info);
            }
            Context.Commit();
            return res;
        }

        #endregion

        #region 删除岗位

        /// <summary>
        /// 删除岗位
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Logging("删除组织层级“{@p0}”")]
        public Result DeleteOrgLevel(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "Id参数不可为空！";
                return res;
            }

            _orgLevelRepository.Remove(ids);

            return res;
        }

        #endregion

        #region 获取一条主数据

        /// <summary>
        /// 获取一条主数据
        /// </summary>
        /// <returns></returns>
        public OrgLevelDTO GetOrgLevelInfo(Guid id)
        {
            return _orgLevelRepository.Get(id).To<OrgLevelDTO>();
        }

        #endregion

        #region 获取分页数据

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<OrgLevelDTO> GetOrgLevelPaged(PagingFeature pf)
        {
            var paged = _orgLevelRepository.Pagging(pf, x => true);

            var output = paged.To<OrgLevel, OrgLevelDTO>();

            return output;
        }

        #endregion

        #endregion

        /// <summary>
        /// 获取组织层级
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<OrgLevelDTO>> GetOrgLevelList()
        {
            return Task.Run(() =>
            {
                return _orgLevelRepository.GetList().To<OrgLevel, OrgLevelDTO>();
            });
        }
    }
}
